package com.monitor.action.admin;

import java.io.PrintWriter;
import java.util.Date;

import com.monitor.action.MonitorActionSupport;
import com.monitor.po.User;
import com.monitor.service.UserService;
import com.monitor.util.Constants;
import com.monitor.util.Pager;

public class UserManageAction extends MonitorActionSupport {

	private static final long serialVersionUID = 8002627222810951993L;
	private UserService userService;
	private Pager<User> users;
	private int cPage;
	private long uid;
	private boolean shield = false;
	private int startHour;
	private int startMinute;
	private int endHour;
	private int endMinute;

	@Override
	public String execute() throws Exception {
		cPage = cPage < 1 ? 1 : cPage;
		users = userService.getUser(Constants.DEFAULT_PAGESIEZE, cPage);
		return SUCCESS;
	}

	public void deleteUser() throws Exception {
		PrintWriter out = getWriter();
		String saveDir = getSaveDirName();
		int count = userService.deleteUserById(uid, saveDir);
		if (count > 0) {
			out.print("success");
		} else {
			out.print("failed");
		}
		out.flush();
		out.close();
	}

	@SuppressWarnings("deprecation")
	public void updateSmsShieldTime() throws Exception {
		PrintWriter out = getWriter();
		Date startTime = null;
		Date endTime = null;
		if (shield) {
			startTime = new Date();
			startTime.setHours(startHour);
			startTime.setMinutes(startMinute);
			endTime = new Date();
			endTime.setHours(endHour);
			endTime.setMinutes(endMinute);
		}
		if (userService.updateSmsShieldedTime(uid, shield, startTime, endTime) > 0) {
			out.print("success");
		} else {
			out.print("failed");
		}
		out.flush();
		out.close();
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public Pager<User> getUsers() {
		return users;
	}

	public void setUsers(Pager<User> users) {
		this.users = users;
	}

	public int getcPage() {
		return cPage;
	}

	public void setcPage(int cPage) {
		this.cPage = cPage;
	}

	public long getUid() {
		return uid;
	}

	public void setUid(long uid) {
		this.uid = uid;
	}

	public boolean isShield() {
		return shield;
	}

	public void setShield(boolean shield) {
		this.shield = shield;
	}

	public int getStartHour() {
		return startHour;
	}

	public void setStartHour(int startHour) {
		this.startHour = startHour;
	}

	public int getStartMinute() {
		return startMinute;
	}

	public void setStartMinute(int startMinute) {
		this.startMinute = startMinute;
	}

	public int getEndHour() {
		return endHour;
	}

	public void setEndHour(int endHour) {
		this.endHour = endHour;
	}

	public int getEndMinute() {
		return endMinute;
	}

	public void setEndMinute(int endMinute) {
		this.endMinute = endMinute;
	}

}
